package httpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class GetDisk
{
	private HttpMessage httpMessage;
	private StringBuilder result;
	private Path path;
	public GetDisk(HttpMessage httpMessage)
	{
		result=new StringBuilder();
		this.httpMessage=httpMessage;
		
		path=Paths.get(this.httpMessage.root,this.httpMessage.targetFile);
		
	}
	
	public void work(OutputStream out)
	{
		
		if(!Files.exists(path))
		{
			result.append("HTTP/1.1 404 Not Found\r\nConnection: close\r\n");
			result.append("\r\n");
			
			try {
				out.write(result.toString().getBytes());
				out.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return;
			
		}
		
		
		byte[] fileContents=null;
		try {
			fileContents=Files.readAllBytes(path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         if(fileContents!=null)
		{
        	result.append("HTTP/1.1 200 OK\r\n");
        	if(httpMessage.targetFile.endsWith("html"))
        	{
        		result.append( "Content-Type: text/html; charset=utf-8\r\n");
        	}
        	else if(httpMessage.targetFile.endsWith("jpg"))
        	{
        		result.append( "Content-Type: image/jpeg\r\n");
        	}
        	result.append( "Keep-Alive: -1\r\n");
        	
     			
			result.append("\r\n");
			
			try {
				out.write(result.toString().getBytes());
				out.write(fileContents);
				out.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}	
		
		
		return;

	}
}
